
import 'dart:io';

import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/sift/model/models.dart';
import 'package:nft_shop/util/other_utils.dart';
import 'package:nft_shop/widgets/my_button.dart';
import 'package:r_upgrade/r_upgrade.dart';

class AppUpdate extends StatefulWidget {
  AppInfo info;

  AppUpdate(this.info);

  @override
  _AppUpdateState createState() => _AppUpdateState();
}

class _AppUpdateState extends State<AppUpdate> {
  bool    isOpen = false;
  double  percent = 0.0;
  @override
  Widget build(BuildContext context) {
    final Widget content = Material(
      borderRadius: BorderRadius.circular(10.0),
      child: Padding(
        padding:
        const EdgeInsets.only(top: 20, left: 20, bottom: 30, right: 20),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              isOpen?"正在更新":"检测有新版本",
              style: TextStyle(
                  fontSize: 16,
                  fontFamily: "PingFangSC-Semibold",
                  color: Color(0xFF131415)),
            ),
            SizedBox(
              height: 20,
            ),
            Visibility(
              visible: !isOpen,
              child: Text(
                widget.info.remarks!,
                style: TextStyle(
                    fontSize: 12,
                    fontFamily: "PingFangSC-Regular",
                    color: Color(0xFF222222)),
              ),
            ),
            Visibility(
              visible: isOpen,
              child: ClipRRect(
                borderRadius: BorderRadius.circular(4),
                child: LinearProgressIndicator(
                  minHeight:8,
                  value: percent,
                  valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF2F73FD)),
                  backgroundColor: Color(0x262F73FD),
                ),
              ),
            ),SizedBox(
              height: 20,
            ),
            Visibility(
              visible: !isOpen,
              child: Container(
                width: 120,
                height: 40,
                decoration: BoxDecoration(
                ),
                child: MyButton(
                  onPressed: () async {
                    if(Platform.isAndroid){
                      setState(() {
                        isOpen = true;
                      });
                      await RUpgrade.upgrade(
                          widget.info.versionUrl!,
                          notificationVisibility: NotificationVisibility.VISIBILITY_HIDDEN,
                          isAutoRequestInstall: true,
                          fileName: 'Nft-update${widget.info.version}.apk');
                      RUpgrade.stream.listen((DownloadInfo info){
                        print("--升级中----${info.percent}");
                        setState(() {
                          percent = info.percent!*0.01;
                        });
                      });
                    }else{
                      Utils.launchWebURL(widget.info.versionUrl!);
                    }
                  },
                  text: "立即更新",
                  radius: 7,
                  fontSize: 16,
                  textColor: Colors.white,
                  backgroundColor: Global.kTheme.colorBackground3,
                ),
              ),
            ),
          ],
        ),
      ),
    );

    final Widget body = MediaQuery.removeViewInsets(
      removeLeft: true,
      removeTop: true,
      removeRight: true,
      removeBottom: true,
      context: context,
      child: Center(
        child: SizedBox(
          width: Get.width - 80,
          child: content,
        ),
      ),
    );

    return WillPopScope(
      onWillPop: () async => Future.value(false),
      child: Scaffold(
        backgroundColor: Colors.transparent, //透明类型
        body: body,
      ),
    );
  }
}